package com.situ.mall.order.controller;


import com.situ.mall.common.entity.MemberAddress;
import com.situ.mall.common.service.IMemberAddressService;
import com.situ.mall.order.service.ICartService;
import com.situ.mall.common.vo.CartVO;
import com.situ.mall.order.vo.LayuiTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author membergpf
 * @since 2023-03-18
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private  ICartService cartService;
    @Autowired
    private IMemberAddressService memberAddressService;

    @GetMapping("/page")
    public String page(){

        System.out.println("cart");
        return "cart";
    }

    @GetMapping("/list")

    public LayuiTableVO list(@RequestParam Integer page, @RequestParam Integer limit ){
        LayuiTableVO vo=new LayuiTableVO();
              vo= cartService.selectAll(page,limit);

                return vo;
    }

    @GetMapping("/list2")
    @ResponseBody
    public List<CartVO> list2(){
        return cartService.selectList();
    }

    //点击购物车页面的去结算跳转settle
  @RequestMapping("/settle")
        public String settle(){
        return "";
    }
    @PostMapping("/del")
    @ResponseBody
    public void del(Integer cartid){

        cartService.delete(cartid);
    }
    @PostMapping("/updateAmount")
    @ResponseBody
    public void updateAmount(Integer cartid,Integer num){
        System.out.println(cartid+""+num);
        cartService.updateAmount(cartid,num);
    }
    @PostMapping("/confirm")
    public String confirm(String ids, Model model){
        //转发个人发货地址
        List<MemberAddress> adds=memberAddressService.list();
        model.addAttribute("adds",adds);
        model.addAttribute("ids",ids);
        //转发选中的商品信息
        List<CartVO> carts=cartService.list1(ids);
        model.addAttribute("carts",carts);
        return "confirm";
    }
}
